package com.rupeng.web.interceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.util.WebUtils;

import com.rupeng.pojo.AdminUser;
import com.rupeng.service.AdminUserService;
import com.rupeng.util.JedisUtils;

public class KeepOnlineInterceptor extends HandlerInterceptorAdapter {
	@Autowired
	private AdminUserService adminUserService;
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		//先判断当前session中有没有adminUser对象
		AdminUser adminUser = (AdminUser)request.getSession().getAttribute("adminUser");
		if (adminUser==null) {
			Cookie sessionIdCookie = WebUtils.getCookie(request, "JSESSIONID");
			if (sessionIdCookie == null) {
				return true;
			}
			String oldSessionId = sessionIdCookie.getValue();
			//从redis服务器中拿到对应的userId
			String userId = JedisUtils.get("keepOnline_"+oldSessionId);
			if (userId==null) {
				return true;
			}
			adminUser = adminUserService.selectOne(Long.parseLong(userId));
			if (adminUser!=null) {
				request.getSession().setAttribute("adminUser", adminUser);
			}
		} else {
			JedisUtils.setex("keepOnline_"+request.getSession().getId(), 60*60*24, adminUser.getId()+"");
		}
		return true;
	}
}
